//
// App.java
//
package com.photoexpert;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.SplashScreen;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;

class App extends javax.swing.JFrame {

    static final Logger _logger = Logger.getLogger("PhotoExpert");

    static App Instance() {
        if(_instance == null)
            _instance = new App();
        return _instance;
    }

    void fileOpened(File f) {
        _file = f;
        enableMenus(true);
        setTitle("Photo Expert - " + (f == null ? "Untitled" : f.getName()));
    }

    void fileClosed() {
        _file = null;
        enableMenus(false);
        setTitle("Photo Expert");
    }

    void historyChanged(int size) {
        _undoMenu.setEnabled(size > 0);
        _undoButton.setEnabled(size > 0);
    }

    void selectionBecomeValid(boolean valid) {
        _cutMenu.setEnabled(valid);
        _copyMenu.setEnabled(valid);
        _cutButton.setEnabled(valid);
        _copyButton.setEnabled(valid);
        _deleteMenu.setEnabled(valid);
        _trimButton.setEnabled(valid);
    }

    void mouseModeChanged() {
        switch(_presentor.getMouseMode()) {
            case SELECTING_RECT :
                if(!_selectRectMenu.isSelected())
                    _selectRectMenu.setSelected(true);
                if(_selectTriangleMenu.isSelected())
                    _selectTriangleMenu.setSelected(false);
                _selectButton.setIcon(_sel_rect);
                break;
            case SELECTING_TRI :
                _selectButton.setIcon(_sel_tri);
                break;
            default :
                _selectRectMenu.setSelected(false);
                _selectTriangleMenu.setSelected(false);
                _selectButton.setIcon(_sel_none);
                break;
        }
    }

    /* ----------------------------------------------------------------------*/

    private App() {
        final SplashScreen splash = SplashScreen.getSplashScreen();
        initComponents();
        _implementor = Implementor.Instance();
        _presentor = Presentor.Instance();
        _implementor.setPresentor(_presentor);
        MDrawer drawer = MDrawer.Instance();
        MRectangleSelector rectSelector = MRectangleSelector.Instance();
        MTriangleSelector triSelector = MTriangleSelector.Instance();
        MDragger dragger = MDragger.Instance();
        MPaster paster = MPaster.Instance();
        // toolbar
        _toolbar = new JToolBar("Toolbar");
        _toolbar.setFocusable(false);
        _newButton = new JButton();
        _newButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _newMenuActionPerformed(null);
            }
        });
        addToolbarButton(_newButton, "New image", "new.jpg", "New");
        _openButton = new JButton();
        _openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _openMenuActionPerformed(null);
            }
        });
        addToolbarButton(_openButton, "Open image", "open.jpg", "Open");
        _saveButton = new JButton();
        _saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _saveMenuActionPerformed(null);
            }
        });
        addToolbarButton(_saveButton, "Save file", "save.jpg", "Save");
        _toolbar.addSeparator();
        _selectButton = new JButton();
        _selectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(_selectRectMenu.isSelected() || _selectTriangleMenu.isSelected()) {
                    _selectRectMenu.setSelected(false);
                    _selectTriangleMenu.setSelected(false);
                } else
                    _selectRectMenu.setSelected(true);
                _selectRectMenuActionPerformed(null);
            }
        });
        addToolbarButton(_selectButton, "Select", "sel_none.jpg", "Select");
        // also load optional images for _selectButton
        _sel_none = loadImage("sel_none.jpg", "Select");
        _sel_rect = loadImage("sel_rect.jpg", "Select");
        _sel_tri = loadImage("sel_tri.jpg", "Select");
        _cutButton = new JButton();
        _cutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _cutMenuActionPerformed(null);
            }
        });
        addToolbarButton(_cutButton, "Cut", "cut.jpg", "Cut");
        _copyButton = new JButton();
        _copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _copyMenuActionPerformed(null);
            }
        });
        addToolbarButton(_copyButton, "Copy", "copy.jpg", "Copy");
        _pasteButton = new JButton();
        _pasteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _pasteMenuActionPerformed(null);
            }
        });
        addToolbarButton(_pasteButton, "Paste", "paste.jpg", "Paste");
        _undoButton = new JButton();
        _undoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _undoMenuActionPerformed(null);
            }
        });
        addToolbarButton(_undoButton, "Undo", "undo.jpg", "Undo");
        _toolbar.addSeparator();
        _zoomOutButton = new JButton();
        _zoomOutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _zoomOutMenuActionPerformed(null);
            }
        });
        addToolbarButton(_zoomOutButton, "ZoomOut", "zoomout.jpg", "ZoomOut");
        _zoomInButton = new JButton();
        _zoomInButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _zoomInMenuActionPerformed(null);
            }
        });
        addToolbarButton(_zoomInButton, "ZoomIn", "zoomin.jpg", "ZoomIn");
        _fitScreenButton = new JButton();
        _fitScreenButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _fitScreenMenuActionPerformed(null);
            }
        });
        addToolbarButton(_fitScreenButton, "Fit", "fitscreen.jpg", "Fit");
        _100PercentButton = new JButton();
        _100PercentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _100PercentMenuActionPerformed(null);
            }
        });
        addToolbarButton(_100PercentButton, "100Percent", "100percent.jpg", "100Percent");
        _toolbar.addSeparator();
        _trimButton = new JButton();
        _trimButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _trimMenuActionPerformed(null);
            }
        });
        addToolbarButton(_trimButton, "Trim", "trim.jpg", "Trim");
        _resizeButton = new JButton();
        _resizeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _resizeMenuActionPerformed(null);
            }
        });
        addToolbarButton(_resizeButton, "Resize", "resize.jpg", "Resize");
        _rotateLeftButton = new JButton();
        _rotateLeftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _counterClockwiseMenuActionPerformed(null);
            }
        });
        addToolbarButton(_rotateLeftButton, "Rotate", "rotate_left.jpg", "Rotate");
        _rotateRightButton = new JButton();
        _rotateRightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _clockwiseMenuActionPerformed(null);
            }
        });
        addToolbarButton(_rotateRightButton, "Rotate", "rotate_right.jpg", "Rotate");
        _brightnessButton = new JButton();
        _brightnessButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _brightnessMenuActionPerformed(null);
            }
        });
        addToolbarButton(_brightnessButton, "Brightness", "brightness.jpg", "Brightness");
        _contrastButton = new JButton();
        _contrastButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _contrastMenuActionPerformed(null);
            }
        });
        addToolbarButton(_contrastButton, "Contrast", "contrast.jpg", "Contrast");
        _sharpnessButton = new JButton();
        _sharpnessButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _sharpnessMenuActionPerformed(null);
            }
        });
        addToolbarButton(_sharpnessButton, "Sharpness", "sharp.jpg", "Sharpness");
        _levelButton = new JButton();
        _levelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _levelMenuActionPerformed(null);
            }
        });
        addToolbarButton(_levelButton, "Level", "level.jpg", "Level");
        _curveButton = new JButton();
        _curveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _curveMenuActionPerformed(null);
            }
        });
        addToolbarButton(_curveButton, "Curve", "curve.jpg", "Curve");
        // add mouse cursor point indicator
        _toolbar.addSeparator();
        _cursorLabel = new JLabel();
        _toolbar.add(_cursorLabel);
        CursorIndicator indicator = new CursorIndicator();
        _presentor.addMouseMotionListener(indicator);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(_toolbar, BorderLayout.NORTH);
        Viewer viewer = Viewer.Instance();
        viewer.setOpaque(true);
        getContentPane().add(viewer, BorderLayout.CENTER);
        pack();
        ImageFileFilter filter = new ImageFileFilter();
        _fileChooser.addChoosableFileFilter(filter);
        enableMenus(false);
        if(splash != null)
            splash.close();
    }

    private ImageIcon loadImage(String img, String text) {
        String imgLocation = "images/" + img;
        URL imgURL = App.class.getResource(imgLocation);
        if(imgURL != null)
            return new ImageIcon(imgURL, text);
        return null;
    }

    private void addToolbarButton(JButton button, String tooltip, String img, String text) {
        button.setFocusable(false);
        button.setToolTipText(tooltip);
        ImageIcon imgIcon = loadImage(img, text);
        if(imgIcon != null)
            button.setIcon(imgIcon);
        else
            button.setText(text);
        _toolbar.add(button);
    }

    private void enableMenus(boolean enable) {
        _newMenu.setEnabled(!enable);
        _openMenu.setEnabled(!enable);
        _saveAsMenu.setEnabled(enable);
        _saveMenu.setEnabled(enable);
        _closeMenu.setEnabled(enable);
        // other menus
        _editMenu.setEnabled(enable);
        _viewMenu.setEnabled(enable);
        _adjustMenu.setEnabled(enable);
        _toolsMenu.setEnabled(enable);
        // toolbar buttons
        _newButton.setEnabled(!enable);
        _openButton.setEnabled(!enable);
        _saveButton.setEnabled(enable);
        _selectButton.setEnabled(enable);
        _cutButton.setEnabled(false);
        _copyButton.setEnabled(false);
        _trimButton.setEnabled(false);
        _undoButton.setEnabled(false);
        _pasteButton.setEnabled(enable);
        _zoomOutButton.setEnabled(enable);
        _zoomInButton.setEnabled(enable);
        _fitScreenButton.setEnabled(enable);
        _100PercentButton.setEnabled(enable);
        _resizeButton.setEnabled(enable);
        _rotateLeftButton.setEnabled(enable);
        _rotateRightButton.setEnabled(enable);
        _brightnessButton.setEnabled(enable);
        _contrastButton.setEnabled(enable);
        _sharpnessButton.setEnabled(enable);
        _levelButton.setEnabled(enable);
        _curveButton.setEnabled(enable);
    }

    /*
     * CursorIndicator
     */
    private class CursorIndicator implements MouseMotionListener {
        @Override
        public void mouseDragged(MouseEvent e) {
            movedTo(e.getX(), e.getY());
        }
        @Override
        public void mouseMoved(MouseEvent e) {
            movedTo(e.getX(), e.getY());
        }
        void movedTo(int x, int y) {
            double rate = _presentor.getZoomRate();
            int x1 = (int)(x / rate);
            int y1 = (int)(y / rate);
            if(x1 > _implementor.getWidth())
                x1 = _implementor.getWidth();
            if(y1 > _implementor.getHeight())
                y1 = _implementor.getHeight();
            _cursorLabel.setText(new Integer(x1).toString() + "," + new Integer(y1).toString());
        }
    }

    /*
     * ImageFileFilter
     * filter image files in the open file dialog
     */
    private static class ImageFileFilter extends FileFilter {
        public final static String jpeg = "jpeg";
        public final static String jpg = "jpg";
        public final static String gif = "gif";
        public final static String tiff = "tiff";
        public final static String tif = "tif";
        public final static String png = "png";

        public String getExtension(File f) {
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');
            if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            }
            return ext;
        }

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            String extension = getExtension(f);
            if (extension != null) {
                if (extension.equals(tiff) ||
                    extension.equals(tif) ||
                    extension.equals(gif) ||
                    extension.equals(jpeg) ||
                    extension.equals(jpg) ||
                    extension.equals(png)) {
                    return true;
                } else
                    return false;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return "Image files";
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        _menuBar = new javax.swing.JMenuBar();
        _fileMenu = new javax.swing.JMenu();
        _newMenu = new javax.swing.JMenuItem();
        _openMenu = new javax.swing.JMenuItem();
        _saveMenu = new javax.swing.JMenuItem();
        _saveAsMenu = new javax.swing.JMenuItem();
        _closeMenu = new javax.swing.JMenuItem();
        _separator1 = new javax.swing.JPopupMenu.Separator();
        _exitMenu = new javax.swing.JMenuItem();
        _editMenu = new javax.swing.JMenu();
        _selectMenu = new javax.swing.JMenu();
        _selectRectMenu = new javax.swing.JCheckBoxMenuItem();
        _selectTriangleMenu = new javax.swing.JCheckBoxMenuItem();
        _separator5 = new javax.swing.JPopupMenu.Separator();
        _selectAllMenu = new javax.swing.JMenuItem();
        _separator7 = new javax.swing.JPopupMenu.Separator();
        _cutMenu = new javax.swing.JMenuItem();
        _copyMenu = new javax.swing.JMenuItem();
        _pasteMenu = new javax.swing.JMenuItem();
        _separator2 = new javax.swing.JPopupMenu.Separator();
        _deleteMenu = new javax.swing.JMenuItem();
        _separator10 = new javax.swing.JPopupMenu.Separator();
        _undoMenu = new javax.swing.JMenuItem();
        _viewMenu = new javax.swing.JMenu();
        _attributeMenu = new javax.swing.JMenuItem();
        _histogramMenu = new javax.swing.JMenuItem();
        _separator4 = new javax.swing.JPopupMenu.Separator();
        _rulerMenu = new javax.swing.JMenu();
        _dotRulerMenu = new javax.swing.JRadioButtonMenuItem();
        _cmRulerMenu = new javax.swing.JRadioButtonMenuItem();
        _inchRulerMenu = new javax.swing.JRadioButtonMenuItem();
        _offRulerMenu = new javax.swing.JRadioButtonMenuItem();
        _zoomMenu = new javax.swing.JMenu();
        _zoomOutMenu = new javax.swing.JMenuItem();
        _zoomInMenu = new javax.swing.JMenuItem();
        _fitScreenMenu = new javax.swing.JMenuItem();
        _100PercentMenu = new javax.swing.JMenuItem();
        _adjustMenu = new javax.swing.JMenu();
        _trimMenu = new javax.swing.JMenuItem();
        _resizeMenu = new javax.swing.JMenuItem();
        _rotateMenu = new javax.swing.JMenu();
        _clockwiseMenu = new javax.swing.JMenuItem();
        _counterClockwiseMenu = new javax.swing.JMenuItem();
        _freeRotateMenu = new javax.swing.JMenuItem();
        _separator12 = new javax.swing.JPopupMenu.Separator();
        _flipVerticalMenu = new javax.swing.JMenuItem();
        _flipHorizontalMenu = new javax.swing.JMenuItem();
        _smoothenMenu = new javax.swing.JMenuItem();
        _separator3 = new javax.swing.JPopupMenu.Separator();
        _brightnessMenu = new javax.swing.JMenuItem();
        _contrastMenu = new javax.swing.JMenuItem();
        _sharpnessMenu = new javax.swing.JMenuItem();
        _yiqMenu = new javax.swing.JMenuItem();
        _hsvMenu = new javax.swing.JMenuItem();
        _separator6 = new javax.swing.JPopupMenu.Separator();
        _levelMenu = new javax.swing.JMenuItem();
        _curveMenu = new javax.swing.JMenuItem();
        _toolsMenu = new javax.swing.JMenu();
        _drawObjectMenu = new javax.swing.JMenuItem();
        _drawTextMenu = new javax.swing.JMenuItem();
        _drawFrameMenu = new javax.swing.JMenuItem();
        _builtInFrameMenu = new javax.swing.JMenuItem();
        _helpMenu = new javax.swing.JMenu();
        _aboutMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Photo Expert");

        _fileMenu.setText("File");

        _newMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/new.jpg"))); // NOI18N
        _newMenu.setText("New...");
        _newMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _newMenuActionPerformed(evt);
            }
        });
        _fileMenu.add(_newMenu);

        _openMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/open.jpg"))); // NOI18N
        _openMenu.setText("Open...");
        _openMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _openMenuActionPerformed(evt);
            }
        });
        _fileMenu.add(_openMenu);

        _saveMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/save.jpg"))); // NOI18N
        _saveMenu.setMnemonic('S');
        _saveMenu.setText("Save");
        _saveMenu.setEnabled(false);
        _saveMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _saveMenuActionPerformed(evt);
            }
        });
        _fileMenu.add(_saveMenu);

        _saveAsMenu.setText("Save As...");
        _saveAsMenu.setEnabled(false);
        _saveAsMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _saveAsMenuActionPerformed(evt);
            }
        });
        _fileMenu.add(_saveAsMenu);

        _closeMenu.setText("Close");
        _closeMenu.setEnabled(false);
        _closeMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _closeMenuActionPerformed(evt);
            }
        });
        _fileMenu.add(_closeMenu);
        _fileMenu.add(_separator1);

        _exitMenu.setText("Exit");
        _exitMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _exitMenuActionPerformed(evt);
            }
        });
        _fileMenu.add(_exitMenu);

        _menuBar.add(_fileMenu);

        _editMenu.setText("Edit");
        _editMenu.setEnabled(false);

        _selectMenu.setText("Select");

        _selectRectMenu.setText("Rectangle Area");
        _selectRectMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/sel_rect.jpg"))); // NOI18N
        _selectRectMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _selectRectMenuActionPerformed(evt);
            }
        });
        _selectMenu.add(_selectRectMenu);

        _selectTriangleMenu.setText("Triangle Area");
        _selectTriangleMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/sel_tri.jpg"))); // NOI18N
        _selectTriangleMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _selectTriangleMenuActionPerformed(evt);
            }
        });
        _selectMenu.add(_selectTriangleMenu);
        _selectMenu.add(_separator5);

        _selectAllMenu.setText("All");
        _selectAllMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _selectAllMenuActionPerformed(evt);
            }
        });
        _selectMenu.add(_selectAllMenu);

        _editMenu.add(_selectMenu);
        _editMenu.add(_separator7);

        _cutMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/cut.jpg"))); // NOI18N
        _cutMenu.setText("Cut");
        _cutMenu.setEnabled(false);
        _cutMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cutMenuActionPerformed(evt);
            }
        });
        _editMenu.add(_cutMenu);

        _copyMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/copy.jpg"))); // NOI18N
        _copyMenu.setText("Copy");
        _copyMenu.setEnabled(false);
        _copyMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _copyMenuActionPerformed(evt);
            }
        });
        _editMenu.add(_copyMenu);

        _pasteMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/paste.jpg"))); // NOI18N
        _pasteMenu.setText("Paste");
        _pasteMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _pasteMenuActionPerformed(evt);
            }
        });
        _editMenu.add(_pasteMenu);
        _editMenu.add(_separator2);

        _deleteMenu.setText("Delete");
        _deleteMenu.setEnabled(false);
        _deleteMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _deleteMenuActionPerformed(evt);
            }
        });
        _editMenu.add(_deleteMenu);
        _editMenu.add(_separator10);

        _undoMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/undo.jpg"))); // NOI18N
        _undoMenu.setText("Undo");
        _undoMenu.setEnabled(false);
        _undoMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _undoMenuActionPerformed(evt);
            }
        });
        _editMenu.add(_undoMenu);

        _menuBar.add(_editMenu);

        _viewMenu.setText("View");
        _viewMenu.setEnabled(false);

        _attributeMenu.setText("Attributes");
        _attributeMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _attributeMenuActionPerformed(evt);
            }
        });
        _viewMenu.add(_attributeMenu);

        _histogramMenu.setText("Histogram");
        _histogramMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _histogramMenuActionPerformed(evt);
            }
        });
        _viewMenu.add(_histogramMenu);
        _viewMenu.add(_separator4);

        _rulerMenu.setText("Ruler");

        buttonGroup1.add(_dotRulerMenu);
        _dotRulerMenu.setSelected(true);
        _dotRulerMenu.setText("Dot");
        _dotRulerMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _dotRulerMenuActionPerformed(evt);
            }
        });
        _rulerMenu.add(_dotRulerMenu);

        buttonGroup1.add(_cmRulerMenu);
        _cmRulerMenu.setText("Centimeter");
        _cmRulerMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cmRulerMenuActionPerformed(evt);
            }
        });
        _rulerMenu.add(_cmRulerMenu);

        buttonGroup1.add(_inchRulerMenu);
        _inchRulerMenu.setText("Inch");
        _inchRulerMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _inchRulerMenuActionPerformed(evt);
            }
        });
        _rulerMenu.add(_inchRulerMenu);

        buttonGroup1.add(_offRulerMenu);
        _offRulerMenu.setText("Off");
        _offRulerMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _offRulerMenuActionPerformed(evt);
            }
        });
        _rulerMenu.add(_offRulerMenu);

        _viewMenu.add(_rulerMenu);

        _zoomMenu.setText("Zoom");

        _zoomOutMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/zoomout.jpg"))); // NOI18N
        _zoomOutMenu.setText("Zoom Out");
        _zoomOutMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _zoomOutMenuActionPerformed(evt);
            }
        });
        _zoomMenu.add(_zoomOutMenu);

        _zoomInMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/zoomin.jpg"))); // NOI18N
        _zoomInMenu.setText("Zoom In");
        _zoomInMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _zoomInMenuActionPerformed(evt);
            }
        });
        _zoomMenu.add(_zoomInMenu);

        _fitScreenMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/fitscreen.jpg"))); // NOI18N
        _fitScreenMenu.setText("Fit to Screen");
        _fitScreenMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _fitScreenMenuActionPerformed(evt);
            }
        });
        _zoomMenu.add(_fitScreenMenu);

        _100PercentMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/100percent.jpg"))); // NOI18N
        _100PercentMenu.setText("Original Size");
        _100PercentMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _100PercentMenuActionPerformed(evt);
            }
        });
        _zoomMenu.add(_100PercentMenu);

        _viewMenu.add(_zoomMenu);

        _menuBar.add(_viewMenu);

        _adjustMenu.setText("Adjust");
        _adjustMenu.setEnabled(false);

        _trimMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/trim.jpg"))); // NOI18N
        _trimMenu.setText("Trim");
        _trimMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _trimMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_trimMenu);

        _resizeMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/resize.jpg"))); // NOI18N
        _resizeMenu.setText("Resize...");
        _resizeMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _resizeMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_resizeMenu);

        _rotateMenu.setText("Rotate");

        _clockwiseMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/rotate_right.jpg"))); // NOI18N
        _clockwiseMenu.setText("Clockwise");
        _clockwiseMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _clockwiseMenuActionPerformed(evt);
            }
        });
        _rotateMenu.add(_clockwiseMenu);

        _counterClockwiseMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/rotate_left.jpg"))); // NOI18N
        _counterClockwiseMenu.setText("Counter clockwise");
        _counterClockwiseMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _counterClockwiseMenuActionPerformed(evt);
            }
        });
        _rotateMenu.add(_counterClockwiseMenu);

        _freeRotateMenu.setText("Free rotation");
        _freeRotateMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _freeRotateMenuActionPerformed(evt);
            }
        });
        _rotateMenu.add(_freeRotateMenu);
        _rotateMenu.add(_separator12);

        _flipVerticalMenu.setText("Flip vertically");
        _flipVerticalMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _flipVerticalMenuActionPerformed(evt);
            }
        });
        _rotateMenu.add(_flipVerticalMenu);

        _flipHorizontalMenu.setText("Flip horizontally");
        _flipHorizontalMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _flipHorizontalMenuActionPerformed(evt);
            }
        });
        _rotateMenu.add(_flipHorizontalMenu);

        _adjustMenu.add(_rotateMenu);

        _smoothenMenu.setText("Smoothen");
        _smoothenMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _smoothenMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_smoothenMenu);
        _adjustMenu.add(_separator3);

        _brightnessMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/brightness.jpg"))); // NOI18N
        _brightnessMenu.setText("Brightness...");
        _brightnessMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _brightnessMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_brightnessMenu);

        _contrastMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/contrast.jpg"))); // NOI18N
        _contrastMenu.setText("Contrast...");
        _contrastMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _contrastMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_contrastMenu);

        _sharpnessMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/sharp.jpg"))); // NOI18N
        _sharpnessMenu.setText("Sharpness...");
        _sharpnessMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _sharpnessMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_sharpnessMenu);

        _yiqMenu.setText("YIQ...");
        _yiqMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _yiqMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_yiqMenu);

        _hsvMenu.setText("HSV...");
        _hsvMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _hsvMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_hsvMenu);
        _adjustMenu.add(_separator6);

        _levelMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/level.jpg"))); // NOI18N
        _levelMenu.setText("Level...");
        _levelMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _levelMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_levelMenu);

        _curveMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photoexpert/images/curve.jpg"))); // NOI18N
        _curveMenu.setText("Curve...");
        _curveMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _curveMenuActionPerformed(evt);
            }
        });
        _adjustMenu.add(_curveMenu);

        _menuBar.add(_adjustMenu);

        _toolsMenu.setText("Tools");
        _toolsMenu.setEnabled(false);

        _drawObjectMenu.setText("Draw object...");
        _drawObjectMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _drawObjectMenuActionPerformed(evt);
            }
        });
        _toolsMenu.add(_drawObjectMenu);

        _drawTextMenu.setText("Draw text...");
        _drawTextMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _drawTextMenuActionPerformed(evt);
            }
        });
        _toolsMenu.add(_drawTextMenu);

        _drawFrameMenu.setText("Draw frame...");
        _drawFrameMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _drawFrameMenuActionPerformed(evt);
            }
        });
        _toolsMenu.add(_drawFrameMenu);

        _builtInFrameMenu.setText("Built-in frames");
        _builtInFrameMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _builtInFrameMenuActionPerformed(evt);
            }
        });
        _toolsMenu.add(_builtInFrameMenu);

        _menuBar.add(_toolsMenu);

        _helpMenu.setText("Help");

        _aboutMenu.setText("About");
        _aboutMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _aboutMenuActionPerformed(evt);
            }
        });
        _helpMenu.add(_aboutMenu);

        _menuBar.add(_helpMenu);

        setJMenuBar(_menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _openMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__openMenuActionPerformed
        int returnVal = _fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION)
            _implementor.openFile(_fileChooser.getSelectedFile());
    }//GEN-LAST:event__openMenuActionPerformed

    private void _saveMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__saveMenuActionPerformed
        _presentor.finishPendingOperation();
        if(_file != null)
            _implementor.saveAs(_file);
        else
            _saveAsMenuActionPerformed(evt);
    }//GEN-LAST:event__saveMenuActionPerformed

    private void _saveAsMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__saveAsMenuActionPerformed
        _presentor.finishPendingOperation();
        int returnVal = App.Instance()._fileChooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            _file = App.Instance()._fileChooser.getSelectedFile();
            _implementor.saveAs(_file);
            setTitle("Photo Expert - " + _file.getName());
        }
    }//GEN-LAST:event__saveAsMenuActionPerformed

    private void _closeMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__closeMenuActionPerformed
        _presentor.finishPendingOperation();
        _presentor.setMouseMode(Presentor.MOUSE_MODES.DRAGGING);
        _implementor.closeFile();
    }//GEN-LAST:event__closeMenuActionPerformed

    private void _exitMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__exitMenuActionPerformed
        System.exit(0);
    }//GEN-LAST:event__exitMenuActionPerformed

    private void _dotRulerMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__dotRulerMenuActionPerformed
        Viewer.Instance().showRuler(true);
        Ruler.setUnit(Ruler.UNIT_MODES.DOT);
    }//GEN-LAST:event__dotRulerMenuActionPerformed

    private void _cmRulerMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cmRulerMenuActionPerformed
        Viewer.Instance().showRuler(true);
        Ruler.setUnit(Ruler.UNIT_MODES.CENTIMETER);
    }//GEN-LAST:event__cmRulerMenuActionPerformed

    private void _inchRulerMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__inchRulerMenuActionPerformed
        Viewer.Instance().showRuler(true);
        Ruler.setUnit(Ruler.UNIT_MODES.INCH);
    }//GEN-LAST:event__inchRulerMenuActionPerformed

    private void _trimMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__trimMenuActionPerformed
        _implementor.trim();
    }//GEN-LAST:event__trimMenuActionPerformed

    private void _resizeMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__resizeMenuActionPerformed
        _presentor.finishPendingOperation();
        ResizeDialog.Instance().display();
    }//GEN-LAST:event__resizeMenuActionPerformed

    private void _brightnessMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__brightnessMenuActionPerformed
        _presentor.finishPendingOperation();
        BrightnessDialog dlg = new BrightnessDialog();
    }//GEN-LAST:event__brightnessMenuActionPerformed

    private void _contrastMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__contrastMenuActionPerformed
        _presentor.finishPendingOperation();
        ContrastDialog dlg = new ContrastDialog();
    }//GEN-LAST:event__contrastMenuActionPerformed

    private void _attributeMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__attributeMenuActionPerformed
        _presentor.finishPendingOperation();
        AttributeDialog.Instance().display();
    }//GEN-LAST:event__attributeMenuActionPerformed

    private void _selectRectMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__selectRectMenuActionPerformed
        _presentor.finishPendingOperation();
        _selectTriangleMenu.setSelected(false);
        _presentor.setMouseMode(_selectRectMenu.isSelected() ?
            Presentor.MOUSE_MODES.SELECTING_RECT : Presentor.MOUSE_MODES.DRAGGING);
    }//GEN-LAST:event__selectRectMenuActionPerformed

    private void _zoomOutMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__zoomOutMenuActionPerformed
        double zoomRate = _presentor.getZoomRate();
        zoomRate /= 2;
        _presentor.setZoomRate(zoomRate);
    }//GEN-LAST:event__zoomOutMenuActionPerformed

    private void _zoomInMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__zoomInMenuActionPerformed
        double zoomRate = _presentor.getZoomRate();
        zoomRate *= 2;
        _presentor.setZoomRate(zoomRate);
    }//GEN-LAST:event__zoomInMenuActionPerformed

    private void _fitScreenMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__fitScreenMenuActionPerformed
        _presentor.setZoomRate(0);
    }//GEN-LAST:event__fitScreenMenuActionPerformed

    private void _100PercentMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__100PercentMenuActionPerformed
        _presentor.setZoomRate(1d);
    }//GEN-LAST:event__100PercentMenuActionPerformed

    private void _clockwiseMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__clockwiseMenuActionPerformed
        _presentor.finishPendingOperation();
        _implementor.clockwise();
    }//GEN-LAST:event__clockwiseMenuActionPerformed

    private void _counterClockwiseMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__counterClockwiseMenuActionPerformed
        _presentor.finishPendingOperation();
        _implementor.counterClockwise();
    }//GEN-LAST:event__counterClockwiseMenuActionPerformed

    private void _selectTriangleMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__selectTriangleMenuActionPerformed
        _presentor.finishPendingOperation();
        _selectRectMenu.setSelected(false);
        _presentor.setMouseMode(Presentor.MOUSE_MODES.SELECTING_TRI);
    }//GEN-LAST:event__selectTriangleMenuActionPerformed

    private void _copyMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__copyMenuActionPerformed
        switch(_presentor.getMouseMode()) {
            case SELECTING_RECT :
                MRectangleSelector.Instance().getSelection().copy();
                break;
            case SELECTING_TRI :
                MTriangleSelector.Instance().getSelection().copy();
                break;
        }
        _presentor.setMouseMode(Presentor.MOUSE_MODES.DRAGGING);
    }//GEN-LAST:event__copyMenuActionPerformed

    private void _deleteMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__deleteMenuActionPerformed
        switch(_presentor.getMouseMode()) {
            case SELECTING_RECT :
                _implementor.backup();
                MRectangleSelector.Instance().getSelection().delete();
                break;
            case SELECTING_TRI :
                _implementor.backup();
                MTriangleSelector.Instance().getSelection().delete();
                break;
        }
        _presentor.setMouseMode(Presentor.MOUSE_MODES.DRAGGING);
    }//GEN-LAST:event__deleteMenuActionPerformed

    private void _cutMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cutMenuActionPerformed
        Presentor.MOUSE_MODES mouseMode = _presentor.getMouseMode();
        if(mouseMode != Presentor.MOUSE_MODES.SELECTING_RECT
            && mouseMode != Presentor.MOUSE_MODES.SELECTING_TRI)
            return;
        if(mouseMode == Presentor.MOUSE_MODES.SELECTING_RECT) {
            _implementor.backup();
            RectSelection s = MRectangleSelector.Instance().getSelection();
            if(s.copy())
                s.delete();
        }
        _presentor.setMouseMode(Presentor.MOUSE_MODES.DRAGGING);
    }//GEN-LAST:event__cutMenuActionPerformed

    private void _pasteMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__pasteMenuActionPerformed
        _presentor.finishPendingOperation();
        Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        if(transferable == null || !transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
            JOptionPane.showMessageDialog(this, "No image in clipboard", "Photo Expert",
                JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            Image img = (Image)transferable.getTransferData(DataFlavor.imageFlavor);
            if(img instanceof BufferedImage) {
                BufferedImage bi = (BufferedImage)img;
                MPaster.Instance().startPaste(bi);
                _presentor.setMouseMode(Presentor.MOUSE_MODES.PASTING);
            }
        } catch (UnsupportedFlavorException ex) {
            JOptionPane.showMessageDialog(this, "Unknown type in clipboard.", "Photo Expert",
                JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) { }
    }//GEN-LAST:event__pasteMenuActionPerformed

    private void _sharpnessMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__sharpnessMenuActionPerformed
        _presentor.finishPendingOperation();
        SharpnessDialog dlg = new SharpnessDialog();
    }//GEN-LAST:event__sharpnessMenuActionPerformed

    private void _levelMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__levelMenuActionPerformed
        _presentor.finishPendingOperation();
        LevelDialog dlg = new LevelDialog();
    }//GEN-LAST:event__levelMenuActionPerformed

    private void _curveMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__curveMenuActionPerformed
        _presentor.finishPendingOperation();
        CurveDialog dlg = new CurveDialog();
    }//GEN-LAST:event__curveMenuActionPerformed

    private void _drawFrameMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__drawFrameMenuActionPerformed
        _presentor.finishPendingOperation();
        StraightFrameDialog dlg = new StraightFrameDialog();
    }//GEN-LAST:event__drawFrameMenuActionPerformed

    private void _builtInFrameMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__builtInFrameMenuActionPerformed
        _presentor.finishPendingOperation();
        BuiltInFrameDialog dlg= new BuiltInFrameDialog();
    }//GEN-LAST:event__builtInFrameMenuActionPerformed

    private void _aboutMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__aboutMenuActionPerformed
        AboutDialog dlg = new AboutDialog();
    }//GEN-LAST:event__aboutMenuActionPerformed

    private void _undoMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__undoMenuActionPerformed
        Implementor.Instance().undo();
    }//GEN-LAST:event__undoMenuActionPerformed

    private void _drawTextMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__drawTextMenuActionPerformed
        _presentor.finishPendingOperation();
        TextDialog dlg = new TextDialog();
    }//GEN-LAST:event__drawTextMenuActionPerformed

    private void _drawObjectMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__drawObjectMenuActionPerformed
        _presentor.finishPendingOperation();
        DrawerDialog dlg = new DrawerDialog();
    }//GEN-LAST:event__drawObjectMenuActionPerformed

    private void _freeRotateMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__freeRotateMenuActionPerformed
        _presentor.finishPendingOperation();
        RotateDialog dlg = new RotateDialog();
    }//GEN-LAST:event__freeRotateMenuActionPerformed

    private void _flipVerticalMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__flipVerticalMenuActionPerformed
        _presentor.finishPendingOperation();
        _implementor.flipVertical();
    }//GEN-LAST:event__flipVerticalMenuActionPerformed

    private void _flipHorizontalMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__flipHorizontalMenuActionPerformed
        _presentor.finishPendingOperation();
        _implementor.flipHorizontal();
    }//GEN-LAST:event__flipHorizontalMenuActionPerformed

    private void _smoothenMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__smoothenMenuActionPerformed
        _presentor.finishPendingOperation();
        _implementor.smoothen();
    }//GEN-LAST:event__smoothenMenuActionPerformed

    private void _yiqMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__yiqMenuActionPerformed
        _presentor.finishPendingOperation();
        YIQDialog dlg = new YIQDialog();
    }//GEN-LAST:event__yiqMenuActionPerformed

    private void _newMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__newMenuActionPerformed
        NewImageDialog.Instance().setVisible(true);
    }//GEN-LAST:event__newMenuActionPerformed

    private void _offRulerMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__offRulerMenuActionPerformed
        Viewer.Instance().showRuler(false);
    }//GEN-LAST:event__offRulerMenuActionPerformed

    private void _histogramMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__histogramMenuActionPerformed
        _presentor.finishPendingOperation();
        HistogramDialog dlg = new HistogramDialog();
    }//GEN-LAST:event__histogramMenuActionPerformed

    private void _selectAllMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__selectAllMenuActionPerformed
        _presentor.finishPendingOperation();
        _presentor.setMouseMode(Presentor.MOUSE_MODES.SELECTING_RECT);
        MRectangleSelector.Instance().selectAll();
    }//GEN-LAST:event__selectAllMenuActionPerformed

    private void _hsvMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__hsvMenuActionPerformed
        _presentor.finishPendingOperation();
        HSVDialog dlg = new HSVDialog();
    }//GEN-LAST:event__hsvMenuActionPerformed

    public static void main(String args[]) {
        try {
            File f = new File("%t/PhotoExpert.err");
            PrintStream ps = new PrintStream(new FileOutputStream(f));
            System.setErr(ps);
            System.setOut(ps);
        }
        catch(FileNotFoundException e) {}
        _logger.setLevel(Level.ALL);
        try {
            FileHandler fh = new FileHandler("%t/PhotoExpert.log");
            fh.setFormatter(new SimpleFormatter());
            _logger.addHandler(fh);
        }
        catch (IOException e) {}
        try {
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        }
        catch (UnsupportedLookAndFeelException e) { }
        catch (ClassNotFoundException e) { }
        catch (InstantiationException e) { }
        catch (IllegalAccessException e) { }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                App.Instance().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem _100PercentMenu;
    private javax.swing.JMenuItem _aboutMenu;
    private javax.swing.JMenu _adjustMenu;
    private javax.swing.JMenuItem _attributeMenu;
    private javax.swing.JMenuItem _brightnessMenu;
    private javax.swing.JMenuItem _builtInFrameMenu;
    private javax.swing.JMenuItem _clockwiseMenu;
    private javax.swing.JMenuItem _closeMenu;
    private javax.swing.JRadioButtonMenuItem _cmRulerMenu;
    private javax.swing.JMenuItem _contrastMenu;
    private javax.swing.JMenuItem _copyMenu;
    private javax.swing.JMenuItem _counterClockwiseMenu;
    private javax.swing.JMenuItem _curveMenu;
    private javax.swing.JMenuItem _cutMenu;
    private javax.swing.JMenuItem _deleteMenu;
    private javax.swing.JRadioButtonMenuItem _dotRulerMenu;
    private javax.swing.JMenuItem _drawFrameMenu;
    private javax.swing.JMenuItem _drawObjectMenu;
    private javax.swing.JMenuItem _drawTextMenu;
    private javax.swing.JMenu _editMenu;
    private javax.swing.JMenuItem _exitMenu;
    private javax.swing.JMenu _fileMenu;
    private javax.swing.JMenuItem _fitScreenMenu;
    private javax.swing.JMenuItem _flipHorizontalMenu;
    private javax.swing.JMenuItem _flipVerticalMenu;
    private javax.swing.JMenuItem _freeRotateMenu;
    private javax.swing.JMenu _helpMenu;
    private javax.swing.JMenuItem _histogramMenu;
    private javax.swing.JMenuItem _hsvMenu;
    private javax.swing.JRadioButtonMenuItem _inchRulerMenu;
    private javax.swing.JMenuItem _levelMenu;
    private javax.swing.JMenuBar _menuBar;
    private javax.swing.JMenuItem _newMenu;
    private javax.swing.JRadioButtonMenuItem _offRulerMenu;
    private javax.swing.JMenuItem _openMenu;
    private javax.swing.JMenuItem _pasteMenu;
    private javax.swing.JMenuItem _resizeMenu;
    private javax.swing.JMenu _rotateMenu;
    private javax.swing.JMenu _rulerMenu;
    private javax.swing.JMenuItem _saveAsMenu;
    private javax.swing.JMenuItem _saveMenu;
    private javax.swing.JMenuItem _selectAllMenu;
    private javax.swing.JMenu _selectMenu;
    private javax.swing.JCheckBoxMenuItem _selectRectMenu;
    private javax.swing.JCheckBoxMenuItem _selectTriangleMenu;
    private javax.swing.JPopupMenu.Separator _separator1;
    private javax.swing.JPopupMenu.Separator _separator10;
    private javax.swing.JPopupMenu.Separator _separator12;
    private javax.swing.JPopupMenu.Separator _separator2;
    private javax.swing.JPopupMenu.Separator _separator3;
    private javax.swing.JPopupMenu.Separator _separator4;
    private javax.swing.JPopupMenu.Separator _separator5;
    private javax.swing.JPopupMenu.Separator _separator6;
    private javax.swing.JPopupMenu.Separator _separator7;
    private javax.swing.JMenuItem _sharpnessMenu;
    private javax.swing.JMenuItem _smoothenMenu;
    private javax.swing.JMenu _toolsMenu;
    private javax.swing.JMenuItem _trimMenu;
    private javax.swing.JMenuItem _undoMenu;
    private javax.swing.JMenu _viewMenu;
    private javax.swing.JMenuItem _yiqMenu;
    private javax.swing.JMenuItem _zoomInMenu;
    private javax.swing.JMenu _zoomMenu;
    private javax.swing.JMenuItem _zoomOutMenu;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    // End of variables declaration//GEN-END:variables

    private static App      _instance = null;
    private Presentor       _presentor = null;
    private Implementor     _implementor = null;
    private JFileChooser    _fileChooser = new JFileChooser();
    private File            _file = null;
    // tool bar buttons
    private JToolBar        _toolbar;
    private JButton         _newButton;
    private JButton         _openButton;
    private JButton         _saveButton;
    private JButton         _selectButton;
    private ImageIcon       _sel_none;
    private ImageIcon       _sel_rect;
    private ImageIcon       _sel_tri;
    private JButton         _cutButton;
    private JButton         _copyButton;
    private JButton         _pasteButton;
    private JButton         _undoButton;
    private JButton         _zoomOutButton;
    private JButton         _zoomInButton;
    private JButton         _100PercentButton;
    private JButton         _fitScreenButton;
    private JButton         _trimButton;
    private JButton         _resizeButton;
    private JButton         _rotateLeftButton;
    private JButton         _rotateRightButton;
    private JButton         _brightnessButton;
    private JButton         _contrastButton;
    private JButton         _sharpnessButton;
    private JButton         _levelButton;
    private JButton         _curveButton;
    private JLabel          _cursorLabel;
}
